home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zfont.c < prev    next >
C/C++ Source or Header  |  1993-05-27  |  9KB  |  333 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfont.c */
  20. /* Font operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gzstate.h"        /* must precede gxdevice */
  25. #include "gxdevice.h"        /* must precede gxfont */
  26. #include "gschar.h"
  27. #include "gxfont.h"
  28. #include "gxfdir.h"
  29. #include "gxcache.h"
  30. #include "alloc.h"
  31. #include "bfont.h"
  32. #include "dict.h"
  33. #include "iname.h"
  34. #include "packed.h"
  35. #include "save.h"
  36. #include "state.h"
  37. #include "store.h"
  38.  
  39. /* Imported operators */
  40. extern int zcleartomark(P1(os_ptr));
  41.  
  42. /* Forward references */
  43. private int font_param(P2(os_ptr, gs_font **));
  44. private int make_font(P3(os_ptr, const gs_matrix *, const ref *));
  45. private void make_uint_array(P3(os_ptr, const uint *, int));
  46.  
  47. /* The (global) font directory */
  48. gs_font_dir *ifont_dir = 0;        /* needed for buildfont */
  49.  
  50. /* Names of system-known keys in font dictionaries: */
  51. ref name_FontMatrix;            /* needed for buildfont */
  52. ref name_FID;
  53. private ref name_OrigFont;        /* needed for scalefont/makefont */
  54. private ref name_ScaleMatrix;        /* ditto */
  55.  
  56. /* Initialize the font operators */
  57. private void
  58. zfont_init(void)
  59. {    static const names_def fnd[] = {
  60.  
  61.     /* Create the names of the standard elements of */
  62.     /* a font dictionary. */
  63.        { "FontMatrix", &name_FontMatrix },
  64.        { "FID", &name_FID },
  65.        { "OrigFont", &name_OrigFont },
  66.        { "ScaleMatrix", &name_ScaleMatrix },
  67.  
  68.     /* Mark the end of the initialized name list. */
  69.        names_def_end
  70.     };
  71.  
  72.     ifont_dir = gs_font_dir_alloc(&alloc_memory_procs);
  73.     init_names(fnd);
  74. }
  75.  
  76. /* <font> <scale> scalefont <new_font> */
  77. int
  78. zscalefont(register os_ptr op)
  79. {    int code;
  80.     float scale;
  81.     gs_matrix mat;
  82.     if ( (code = num_params(op, 1, &scale)) < 0 ) return code;
  83.     if ( (code = gs_make_scaling(scale, scale, &mat)) < 0 ) return code;
  84.     return make_font(op, &mat, NULL);
  85. }
  86.  
  87. /* <font> <matrix> makefont <new_font> */
  88. int
  89. zmakefont(register os_ptr op)
  90. {    int code;
  91.     gs_matrix mat;
  92.     if ( (code = read_matrix(op, &mat)) < 0 ) return code;
  93.     return make_font(op, &mat, op);
  94. }
  95.  
  96. /* <font> setfont - */
  97. int
  98. zsetfont(register os_ptr op)
  99. {    gs_font *pfont;
  100.     int code = font_param(op, &pfont);
  101.     if ( code < 0 || (code = gs_setfont(igs, pfont)) < 0 )
  102.       return code;
  103.     istate.font = *op;
  104.     pop(1);
  105.     return code;
  106. }
  107.  
  108. /* - currentfont <font> */
  109. int
  110. zcurrentfont(register os_ptr op)
  111. {    push(1);
  112.     *op = istate.font;
  113.     return 0;
  114. }
  115.  
  116. /* - cachestatus <mark> <bsize> <bmax> <msize> <mmax> <csize> <cmax> <blimit> */
  117. int
  118. zcachestatus(register os_ptr op)
  119. {    uint status[7];
  120.     gs_cachestatus(ifont_dir, status);
  121.     push(7);
  122.     make_uint_array(op - 6, status, 7);
  123.     return 0;
  124. }
  125.  
  126. /* <blimit> setcachelimit - */
  127. int
  128. zsetcachelimit(register os_ptr op)
  129. {    long limit;
  130.     check_type(*op, t_integer);
  131.     limit = op->value.intval;
  132.     if ( (ulong)limit > max_uint )    /* also covers limit < 0 */
  133.         return_error(e_rangecheck);
  134.     gs_setcachelimit(ifont_dir, (uint)limit);
  135.     pop(1);
  136.     return 0;
  137. }
  138.  
  139. /* <mark> <size> <lower> <upper> setcacheparams - */
  140. int
  141. zsetcacheparams(register os_ptr op)
  142. {    uint params[2];
  143.     int i, code;
  144.     for ( i = 0; i < 2 && !r_has_type(op - i, t_mark) ; i++ )
  145.        {    long parm;
  146.         check_type(op[-i], t_integer);
  147.         parm = op[-i].value.intval;
  148.         if ( (ulong)parm > max_uint )    /* covers parm < 0 */
  149.             return_error(e_rangecheck);
  150.         params[i] = parm;
  151.        }
  152.     switch ( i )
  153.        {
  154.     case 2:
  155.         if ( (code = gs_setcachelower(ifont_dir, params[1])) < 0 )
  156.             return code;
  157.     case 1:
  158.         if ( (code = gs_setcacheupper(ifont_dir, params[0])) < 0 )
  159.             return code;
  160.     case 0: ;
  161.        }
  162.     return zcleartomark(op);
  163. }
  164.  
  165. /* - currentcacheparams <mark> <size> <lower> <upper> */
  166. int
  167. zcurrentcacheparams(register os_ptr op)
  168. {    uint params[2];
  169.     params[0] = gs_currentcachelower(ifont_dir);
  170.     params[1] = gs_currentcacheupper(ifont_dir);
  171.     push(3);
  172.     make_tv(op - 2, t_mark, intval, 0);
  173.     make_uint_array(op - 1, params, 2);
  174.     return 0;
  175. }
  176.  
  177. /* ------ Initialization procedure ------ */
  178.  
  179. op_def zfont_op_defs[] = {
  180.     {"0currentfont", zcurrentfont},
  181.     {"2makefont", zmakefont},
  182.     {"2scalefont", zscalefont},
  183.     {"1setfont", zsetfont},
  184.     {"0cachestatus", zcachestatus},
  185.     {"1setcachelimit", zsetcachelimit},
  186.     {"1setcacheparams", zsetcacheparams},
  187.     {"0currentcacheparams", zcurrentcacheparams},
  188.     op_def_end(zfont_init)
  189. };
  190.  
  191. /* ------ Subroutines ------ */
  192.  
  193. /* Validate a font parameter. */
  194. private int
  195. font_param(os_ptr fp, gs_font **pfont)
  196. {    /* Check that fp is a read-only dictionary, */
  197.     /* and that it has a FID entry. */
  198.     ref *pid;
  199.     check_type(*fp, t_dictionary);
  200.     if ( dict_find(fp, &name_FID, &pid) <= 0 )
  201.         return_error(e_invalidfont);
  202.     *pfont = pid->value.pfont;
  203.     if ( *pfont == 0 )
  204.         return_error(e_invalidfont); /* unregistered font */
  205.     return 0;
  206. }
  207.  
  208. /* Add the FID entry to a font dictionary. */
  209. int
  210. add_FID(ref *fp /* t_dictionary */,  gs_font *pfont)
  211. {    ref fid;
  212.     make_tv_new(&fid, t_fontID, pfont, pfont);
  213.     return dict_put(fp, &name_FID, &fid);
  214. }
  215.  
  216. /* Make a transformed font (common code for makefont/scalefont). */
  217. private int
  218. make_font(os_ptr op, const gs_matrix *pmat, const ref *pmref)
  219. {    os_ptr fp = op - 1;
  220.     gs_font *oldfont, *newfont, *ffont;
  221.     int code;
  222.     if ( (code = font_param(fp, &oldfont)) < 0 ||
  223.          (code = gs_makefont(ifont_dir, oldfont, pmat,
  224.                  &newfont, &ffont)) < 0
  225.        )
  226.       return code;
  227.     /* Check whether the scaled font was already cached. */
  228.     if ( newfont->client_data == 0 )    /* not in the cache */
  229.     {    ref newdict, newdata, newmat;
  230.         ref mref;
  231. #define font_data_refs (sizeof(font_data) / sizeof(ref))
  232.         uint data_len = font_data_refs + 6;
  233.         uint dlen = dict_maxlength(fp);
  234.         /* Ensure room for FontID, OrigFont, ScaleMatrix. */
  235.         uint mlen = dict_length(fp) + 3;
  236.         if ( pmref == NULL )
  237.         {    /* Prepare to create the matrix implied by scalefont. */
  238.             data_len += 6;
  239.         }
  240.         if ( dlen < mlen )
  241.             dlen = mlen;
  242.         if ( (code = dict_create(dlen, &newdict)) < 0 ||
  243.              (code = dict_copy(fp, &newdict)) < 0 ||
  244.              (code = alloc_array(&newdata, a_all, data_len, "make_font")) < 0
  245.            )
  246.           return code;
  247.         if ( pmref == NULL )
  248.         {    /* Create the scaling matrix now. */
  249.             data_len -= 6;
  250.             refcpy_to_new(newdata.value.refs + data_len,
  251.                       (const ref *)pmat, 6);
  252.             r_set_size(&newdata, data_len);
  253.             make_array(&mref, a_readonly, 6,
  254.                    newdata.value.refs + data_len);
  255.             pmref = &mref;
  256.         }
  257.         make_array(&newmat, a_readonly, 6, newdata.value.refs + font_data_refs);
  258. #undef font_data_refs
  259.         if ( (code = dict_put(&newdict, &name_FontMatrix, &newmat)) < 0 ||
  260.              (code = dict_put(&newdict, &name_OrigFont, fp)) < 0 ||
  261.              (pmref != NULL && (code = dict_put(&newdict, &name_ScaleMatrix, pmref)) < 0) ||
  262.              (code = add_FID(&newdict, newfont)) < 0
  263.            )
  264.             return code;
  265.         newfont->client_data = (char *)newdata.value.refs;
  266.         *(font_data *)newdata.value.refs =
  267.             *(font_data *)(oldfont->client_data);
  268.         ((font_data *)newdata.value.refs)->dict = newdict;
  269.         *(gs_matrix *)newmat.value.refs = newfont->FontMatrix;
  270.         r_clear_attrs(dict_access_ref(&newdict), a_write);
  271.     }
  272.     *fp = ((font_data *)(newfont->client_data))->dict;
  273.     if ( ffont )
  274.       { /****** SHOULD DECREMENT REFCT ******/
  275.       }
  276.     pop(1);
  277.     return 0;
  278. }
  279.  
  280. /* Convert an array of (unsigned) integers to stack form. */
  281. private void
  282. make_uint_array(register os_ptr op, const uint *intp, int count)
  283. {    int i;
  284.     for ( i = 0; i < count; i++, op++, intp++ )
  285.         make_int(op, *intp);
  286. }
  287.  
  288. /* Remove scaled font and character cache entries that would be */
  289. /* invalidated by a restore. */
  290. void
  291. font_restore(const alloc_save *save)
  292. {    gs_font_dir *pdir = ifont_dir;
  293.     if ( pdir == 0 )        /* never initialized */
  294.         return;
  295.  
  296.     /* Purge original (unscaled) fonts. */
  297.  
  298.     {    gs_font *pfont;
  299. otop:        for ( pfont = pdir->orig_fonts; pfont != 0;
  300.               pfont = pfont->next
  301.             )
  302.         { if ( alloc_is_since_save((char *)pfont, save) )
  303.            { gs_purge_font(pfont); goto otop; }
  304.         }
  305.     }
  306.  
  307.     /* Purge scaled fonts. */
  308.  
  309.     {    gs_font *pfont;
  310. top:        for ( pfont = pdir->scaled_fonts; pfont != 0;
  311.               pfont = pfont->next
  312.             )
  313.         { if ( alloc_is_since_save((char *)pfont, save) )
  314.            { gs_purge_font(pfont); goto top; }
  315.         }
  316.     }
  317.  
  318.     /* Purge xfonts. */
  319.  
  320.     {    cached_fm_pair *pair;
  321.         uint n;
  322.         for ( pair = pdir->fmcache.mdata, n = pdir->fmcache.mmax;
  323.               n > 0; pair++, n--
  324.             )
  325.         {    if ( !fm_pair_is_free(pair) &&
  326.                  pair->xfont != 0 &&
  327.                  alloc_is_since_save((char *)pair->xfont, save)
  328.                )
  329.               gs_purge_fm_pair(pdir, pair, 1);
  330.         }
  331.     }
  332. }
  333.